#include <stdio.h> 
#include <sys/ioctl.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <sys/time.h> 
#include <fcntl.h> 
#include <termios.h> 
#include <errno.h> 
#include <pthread.h> 
#include <string.h>
#include <semaphore.h> 
#include <sys/poll.h>
#include <netinet/in.h>
#include <signal.h>

#include "common_head.h"
#include "common.h"
#include "ftp_client_interface.h"


typedef struct 
{
	pthread_rwlock_t lock;
	UPLOAD_PARA_TYPE *up_paras;
	int num;
	int index;
}UP_PARA_TYPE;

static UP_PARA_TYPE sg_ups;


static void* ftp_send_file_thread(void *arg);


int ftp_module_init()
{
	return ftp_init();
}

int ftp_module_exit()
{
	return ftp_exit();
}

int ftp_multi_send_file_debug1()
{
	int num = 1;
	UPLOAD_PARA_TYPE *up_para;
	UPLOAD_PARA_TYPE *up_paras = (UPLOAD_PARA_TYPE*)malloc(sizeof(UPLOAD_PARA_TYPE) * num);

	printf("%s: num = %d\n", __FUNCTION__, num);

	up_para = &(up_paras[0]);
	strcpy(up_para->domain, FTP_SERVER_DOMAIN);
	strcpy(up_para->user, FTP_SERVER_USER);
	strcpy(up_para->passwd, FTP_SERVER_PASSWD);
	strcpy(up_para->local_file, FTP_LOCAL_FILE_DIR);
	strcpy(up_para->remote_file, FTP_REMOTE_FILE_DIR);

	return ftp_multi_send_file(up_paras, num);
}

int ftp_multi_send_file_debug2()
{
	int num = 1;
	UPLOAD_PARA_TYPE *up_para;
	UPLOAD_PARA_TYPE *up_paras = (UPLOAD_PARA_TYPE*)malloc(sizeof(UPLOAD_PARA_TYPE) * num);

	printf("%s: num = %d\n", __FUNCTION__, num);

	up_para = &(up_paras[0]);
	strcpy(up_para->domain, FTP_SERVER_DOMAIN2);
	strcpy(up_para->user, FTP_SERVER_USER2);
	strcpy(up_para->passwd, FTP_SERVER_PASSWD2);
	strcpy(up_para->local_file, FTP_LOCAL_FILE_DIR2);
	strcpy(up_para->remote_file, FTP_REMOTE_FILE_DIR2);

	return ftp_multi_send_file(up_paras, num);
}

int ftp_multi_send_file_debug3()
{
	int num = 1;
	UPLOAD_PARA_TYPE *up_para;
	UPLOAD_PARA_TYPE *up_paras = (UPLOAD_PARA_TYPE*)malloc(sizeof(UPLOAD_PARA_TYPE) * num);

	printf("%s: num = %d\n", __FUNCTION__, num);

	up_para = &(up_paras[0]);
	strcpy(up_para->domain, FTP_SERVER_DOMAIN3);
	strcpy(up_para->user, FTP_SERVER_USER3);
	strcpy(up_para->passwd, FTP_SERVER_PASSWD3);
	strcpy(up_para->local_file, FTP_LOCAL_FILE_DIR3);
	strcpy(up_para->remote_file, FTP_REMOTE_FILE_DIR3);

	return ftp_multi_send_file(up_paras, num);
}

int ftp_multi_send_file_debug4()
{
	int num = 1;
	UPLOAD_PARA_TYPE *up_para;
	UPLOAD_PARA_TYPE *up_paras = (UPLOAD_PARA_TYPE*)malloc(sizeof(UPLOAD_PARA_TYPE) * num);

	printf("%s: num = %d\n", __FUNCTION__, num);

	up_para = &(up_paras[0]);
	strcpy(up_para->domain, FTP_SERVER_DOMAIN4);
	strcpy(up_para->user, FTP_SERVER_USER4);
	strcpy(up_para->passwd, FTP_SERVER_PASSWD4);
	strcpy(up_para->local_file, FTP_LOCAL_FILE_DIR4);
	strcpy(up_para->remote_file, FTP_REMOTE_FILE_DIR4);

	return ftp_multi_send_file(up_paras, num);
}

int ftp_multi_send_file(UPLOAD_PARA_TYPE *up_paras, const int num)
{
	if (!up_paras || num <= 0 || num > MAX_FTP_UPLOAD_NUMBER)
	{
		udi_erro_log("%s: para error ! %p, %d\n", __FUNCTION__, up_paras, num);
		return -1;
	}

	sg_ups.up_paras = up_paras;
	sg_ups.num = num;

	pthread_t pid;
	create_thread_middle(&pid, ftp_send_file_thread, (void*)NULL);

	return 0;
}

static void* ftp_send_file_thread(void *arg)
{
	UPLOAD_PARA_TYPE *up_paras = sg_ups.up_paras;
	int para_num = sg_ups.num;

	if (para_num <= 0 || para_num > MAX_FTP_UPLOAD_NUMBER)
	{
		udi_erro_log("%s: para error ! %d\n", __FUNCTION__, para_num);
		return NULL;
	}

	int i;
	for (i = 0; i < para_num; i ++)
	{
		UPLOAD_PARA_TYPE *up = &up_paras[i];
		//ftp_client_main(up->domain, up->port, up->user, up->passwd, up->local_file, up->remote_file);
	}

	return NULL;
}

int ftp_get_all_send_file_percent(float *percents)
{
	if (!percents)
	{
		udi_erro_log("%s:para error !\n", __FUNCTION__);
		return -1;
	}

	int i;
	pthread_rwlock_rdlock(&(sg_ups.lock));
	if (sg_ups.index < 0 && sg_ups.num <= 0 && sg_ups.index >= sg_ups.num)
	{
		udi_erro_log("%s:para error ! sg_ups.index=%d, sg_ups.num=%d\n", __FUNCTION__, sg_ups.index, sg_ups.num);
		pthread_rwlock_unlock(&(sg_ups.lock));
		return -2;
	}

	//percents[sg_ups.index] = ftp_get_upload_percent();
	for (i = 0; i < sg_ups.index; i++)
	{
		percents[i] = 100.0;
	}

	for (i = sg_ups.index + 1; i < sg_ups.num; i++)
	{
		percents[i] = 0.0;
	}

	pthread_rwlock_unlock(&(sg_ups.lock));

	return 0;
}



